<template>
  <div>
    <router-link tag="div" v-show="showAbs" to="/" class="header-abs">
      <div class="iconfont iconfanhui header-abs-back-icon"></div>
    </router-link>
    <div
      class="header-fixed"
      :style="opacityStyle"
      v-show="!showAbs">
      <router-link to="/">
        <div class="iconfont iconfanhui header-fixed-back"></div>
      </router-link>
      景点详情
    </div>
  </div>
</template>

<script>
  export default {
    name: 'Header',
    data () {
      return {
        showAbs: true,
        opacityStyle: {
          opacity: 0
        }
      }
    },
    watch: {},
    activated () {
      //对全局事件绑定
      window.addEventListener('scroll', this.handleScroll)
    },
    deactivated () {
      //移除全局事件  否则其他页面  会受影响
      window.removeEventListener('scroll', this.handleScroll)
    },
    methods: {
      handleScroll: function () {
        const top = document.documentElement.scrollTop
        console.log(top)
        if (top > 60) {
          let opacity = top / 140
          opacity = opacity > 1 ? 1 : opacity
          this.opacityStyle = {
            opacity
          }
          this.showAbs = false
        } else {
          this.showAbs = true
        }
      }
    }
  }
</script>

<style scoped lang="stylus">
  @import "../../../assets/styles/varibles.styl"
  .header-fixed
    position fixed
    top 0
    z-index 2
    left 0
    right 0
    height $headerHeight
    line-height $headerHeight
    text-align center
    color #ffffff
    background $bgColor
    font-size .32rem

    .header-fixed-back
      top 0
      left 0
      position absolute
      width .42rem
      text-align center
      font-size .4rem
      color #ffffff

  .header-abs
    position absolute
    left .2rem
    top .2rem
    width .8rem
    height .8rem
    border-radius .4rem
    line-height .8rem
    text-align center
    background rgba(0, 0, 0, .8rem)

    .header-abs-back-icon
      color #ffffff
      font-size .4rem
</style>
